package com.uit.trip_.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author duongnguyen
 * 
 */
public class TripDatabaseHelper extends SQLiteOpenHelper {

	public static final String DB_NAME = "TripData";
	public static final int VERSION = 1;

	public static final String TABLE_TRIP = "trip";
	public static final String TRIP_ID = "_id";
	public static final String TRIP_NAME = "name";
	public static final String TRIP_DESCRIPTION = "description";
	public static final String TRIP_DATE = "date";
	// public static final String TRIP_LOCATION = "location";

	public static final String TABLE_NOTE = "note";
	public static final String NOTE_ID = "_id";
	public static final String NOTE_TRIP_ID = "trip_id";
	public static final String NOTE_CONTENT = "content";
	public static final String NOTE_IMAGE_PATH = "path";
	public static final String NOTE_LOCATION = "location";
	public static final String NOTE_DATE = "date";

	public static final String TABLE_IMAGE = "image";
	public static final String IMAGE_ID = "_id";
	public static final String IMAGE_NOTE_ID = "note_id";
	public static final String IMAGE_PATH = "path";

	private static final String CREATE_TRIP_TABLE = "create table "
			+ TABLE_TRIP + "(" + TRIP_ID
			+ " integer primary key autoincrement," + TRIP_DESCRIPTION
			+ " text," + TRIP_NAME + " text not null," + TRIP_DATE + " text)";

	private static final String CREATE_NOTE_TABLE = "create table "
			+ TABLE_NOTE + "(" + NOTE_ID
			+ " integer primary key autoincrement," + NOTE_TRIP_ID
			+ " integer not null," + NOTE_CONTENT + " text, " + NOTE_LOCATION
			+ " text," + NOTE_DATE + " text," + NOTE_IMAGE_PATH + " text)";

	private static final String CREATE_IMAGE_TABLE = "create table "
			+ TABLE_IMAGE + "(" + IMAGE_ID
			+ " integer primary key autoincrement," + IMAGE_NOTE_ID
			+ " integer not null," + IMAGE_PATH + " text)";

	public TripDatabaseHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		Log.i("duongnx", "onCreate");
		db.execSQL(CREATE_TRIP_TABLE);
		db.execSQL(CREATE_NOTE_TABLE);
		db.execSQL(CREATE_IMAGE_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("drop table if exist " + TABLE_TRIP);
		db.execSQL("drop table if exist " + TABLE_NOTE);
		db.execSQL("drop table if exist " + TABLE_IMAGE);
		onCreate(db);
	}
}
